package com.jh.tools.common.notify;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.SystemClock;

import com.jh.tools.R;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by niejiahuan on 2016/3/24.
 * Notify管理类，用于对Notify进行管理
 */
public class NotifyManage {
    private static NotifyManage mNotifyManage;
    private static Map<String,NotifyUtil> notifyUtils;
    private int requestCode = (int) SystemClock.uptimeMillis();
    public static NotifyManage getInstance(){
        if (null==mNotifyManage){
            mNotifyManage=new NotifyManage();
            notifyUtils=new HashMap<>();
        }
        return mNotifyManage;
    }

    public void showNotify(Context mContext, int notifyId, List<String> messageList,String largeIcon, String messageSize, String ticker, String title){
        //设置想要展示的数据内容
//        Intent intent = new Intent(mContext, ChatActivity.class);
//        Bundle data = new Bundle();
//        data.putString(IntentConstant.SEND_CHAT_ACTIVITY_USERID,notifyId+"");
//        data.putString(IntentConstant.SEND_CHAT_ACTIVITY_USERNAME,title);
//        intent.putExtras(data);
//        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//        PendingIntent pIntent = PendingIntent.getActivity(mContext,
//                requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
////        int largeIcon = R.mipmap.ic_launcher;
//        int smallIcon = R.mipmap.msg_icon;
//        String ticker = "您有一条新通知";.
//        String title = "冰冰";
//        messageList.add("文明,今晚有空吗？");
//        messageList.add("晚上跟我一起去玩吧?");
//        messageList.add("怎么不回复我？？我生气了！！");
//        messageList.add("我真生气了！！！！！你听见了吗!");
//        messageList.add("文明，别不理我！！！");
//        String content = "[" + messageSize + "条]" + title + ": " + messageList.get(0);
//        //实例化工具类，并且调用接口
//        NotifyUtil notify3 = new NotifyUtil(mContext, notifyId);
//
//        notify3.notify_mailbox(pIntent, smallIcon, largeIcon, messageList,messageSize, ticker,
//                title, content, true, true, false);
//        boolean isadd=true;
//        for (NotifyUtil notifyUtil:notifyUtils) {
//            if (notifyUtil.getNOTIFICATION_ID()==notify3.getNOTIFICATION_ID()){
//                isadd=false;
//            }
//        }
//        if (isadd){
//            notifyUtils.add(notify3);
//        }

    }

    /**
     * 启动下载进度条
     * @param mContext
     * @param notifyId id
     * @param smallIcon 图标
     * @param ticker  副标题
     * @param title   标题
     */


   public void notify_progress(Context mContext, int notifyId,int smallIcon,String ticker,String title){
       //实例化工具类，并且调用接口
       NotifyUtil notify7 = new NotifyUtil(mContext, notifyId);
       PendingIntent contentIntent = PendingIntent.getActivity(
               mContext, 0, new Intent(), 0);
       notify7.notify_progress(contentIntent, smallIcon, ticker, title, mContext.getString(R.string.start_download), false, false, false);
       notifyUtils.put(notifyId+"",notify7);
   }

    /**
     * 跟新下载的状态
     * @param notifyId
     * @param max
     * @param progress
     * @param saveFilePath
     */
    public void notify_progress_update(String notifyId,int max, int progress,File saveFilePath){
        NotifyUtil notify= notifyUtils.get(notifyId);
        String title=notify.mContext.getString(R.string.start_download);
        if (max>progress){//下载中
            title=notify.mContext.getString(R.string.downloading);
        }else if (max==progress){//下载完成
            title=notify.mContext.getString(R.string.download_success);
            setPendingIntent(notify.mContext,saveFilePath,notify);
        }
        notify.cBuilder.setContentText(title).setProgress(max, progress, false);
        notify.sent();
    }
    public void setPendingIntent(Context mContext,File saveFilePath,NotifyUtil notifyUtil){
        if (null!=notifyUtil){
            //设置想要展示的数据内容
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(saveFilePath),
                    "application/vnd.android.package-archive");
            PendingIntent contentIntent = PendingIntent.getActivity(
                    mContext, 0, intent, 0);
            notifyUtil.cBuilder.setContentIntent(contentIntent);
        }

    }
    /**
     * 根据ID清除相应的Id
     * @param notifyId
     */
    public void clearById(String notifyId){
        notifyUtils.get(notifyId+"").clear();
    }
    /**
     * 清除
     */
    public void clears(){
        if (null!=notifyUtils)
        for (String KEY:notifyUtils.keySet()) {
            clearById(KEY);
        }
    }
}
